package JDBCTools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC 的工具类
 * 
 * @author kw-hls
 *
 */
public class JDBCTools {

	/**
	 * 关闭Statement,Connection
	 * 
	 * @param statement
	 * @param connection
	 */
	public static void release(Statement statement, Connection connection) {
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取数据库连接的方法
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() {
		// 准备连接数据库的四个字符串
		// 驱动的全类名
		String driverClass = "com.mysql.jdbc.Driver";
		String jdbcUrl = "jdbc:mysql://localhost:3306/filemonitor?useUnicode=true&characterEncoding=utf8";
		String username = "root";
		String password = "root";
		// 加载数据库驱动程序(注册驱动)
		try {
			Class.forName(driverClass);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(jdbcUrl, username,
					password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
}